package com.ilop.sthome.ui.activity.detail.lock;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DefaultBean;
import com.ilop.sthome.ui.activity.detail.BaseDetailActivity;
import com.ilop.sthome.ui.adapter.detail.DefaultAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DefaultDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDetailAlarmBinding;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author skygge
 * @date 2019-12-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：门锁详情页
 */
public class LockDetailActivity extends BaseDetailActivity<ActivityDetailAlarmBinding> {

    private AtomicBoolean mFlagActive;
    private DefaultAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_alarm;
    }

    @Override
    protected void initView() {
        super.initView();
        mFlagActive = new AtomicBoolean(false);
        mDBind.detailIcon.setImageResource(R.mipmap.gs920);
        mDBind.operation.setText(getResources().getString(R.string.unlock));
        mAdapter = new DefaultAdapter(mContext);
        mDBind.detailList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.detailList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        buildDefaultAutomation();
        doStatusShow(mDevice.getDevice_status());
    }

    private void buildDefaultAutomation() {
        int defaultSize = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice).size();
        if (defaultSize == 0){
            DefaultDaoUtil.getInstance().setDefaultList(mContext, smartDevice.getDevCommon(), mDevice);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.operation.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, UnlockActivity.class);
            intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
            intent.putExtra(CommonId.KEY_DEVICE_ID, mDeviceId);
            startActivity(intent);
            overridePendingTransition(R.anim.anim_down_in, R.anim.anim_down_out);
        });
    }

    private void doStatusShow(String statusCode) {
        try {
            String signal = statusCode.substring(0,2);
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic(signal));
            int quantity = Integer.parseInt(statusCode.substring(2,4),16);
            String status = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), statusCode);
            int statusColor = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), statusCode);
            showDetail(statusColor, status, quantity);
            String state = statusCode.substring(4,6);
            setLockActive(state);
        }catch (Exception e){
            showDetail(3, getString(R.string.offline), 0);
            mFlagActive.set(false);
        }
    }

    private void showDetail(int statusColor, String text, int quantity){
        if (statusColor == 3){
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic("00"));
            mDBind.quantityPosition.setVisibility(View.GONE);
            mDBind.quantityDesc.setVisibility(View.GONE);
        }else {
            mDBind.quantityPosition.setVisibility(View.VISIBLE);
            mDBind.quantityDesc.setVisibility(View.VISIBLE);
            mDBind.quantityPosition.setImageResource(ConfigureUtil.choseQPic(quantity));
            mDBind.quantityDesc.setText(ConfigureUtil.choseLNum(quantity));
        }
        int background = ConfigureUtil.choseDeviceStateColor(statusColor);
        mDBind.detailLayout.setBackgroundResource(background);
        mDBind.showStatus.setText(text);
        if (statusColor == 0) {
            AnimationDrawable mAnimation = (AnimationDrawable) mDBind.detailLayout.getBackground();
            mAnimation.start();
        }
    }

    private void setLockActive(String status){
        switch (status) {
            case "AB":
            case "55":
                mFlagActive.set(true);
                break;
            default:
                mFlagActive.set(false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(TextUtils.isEmpty(mDevice.getSubdeviceName())){
            String title = getString(R.string.lock) + mDevice.getDevice_ID();
            mDBind.detailName.setText(title);
        }else{
            mDBind.detailName.setText(mDevice.getSubdeviceName());
        }
        refreshDefaultList();
    }

    @Override
    protected void refreshUI(String status) {
        super.refreshUI(status);
        doStatusShow(status);
    }

    @Override
    protected void refreshDefaultList() {
        super.refreshDefaultList();
        List<DefaultBean> defaultBeanList = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice);
        mAdapter.setList(defaultBeanList);
    }
}
